package com.shenda.device.message;

import com.shenda.device.Constants;
import com.shenda.device.Device;
import com.shenda.device.enums.Cmd;
import com.swak.utils.StringUtils;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 基础的响应
 * 
 * @author lifeng
 */
@Getter
@Setter
@Accessors(chain = true)
public abstract class RespMsg extends BaseMsg {

	// 对应的设备
	private Device device;

	/**
	 * 默认是14个字符长度
	 * 
	 * @return
	 */
	public abstract int commandLength();

	/**
	 * 解析字符串
	 * 
	 * @param message
	 * @return
	 */
	public abstract RespMsg parse(byte[] message);

	/**
	 * 创建命令 -- 必须满足AABBCCXX的格式，否则不处理
	 * 
	 * @param cmd
	 * @return
	 */
	public static RespMsg of(String cmd) {
		if (StringUtils.startsWith(cmd, Constants.CMD_Head)) {
			cmd = StringUtils.substringAfter(cmd, Constants.CMD_Head);
			Cmd command = Cmd.cmdOf(cmd);
			// 没找到对应的命令
			if (command == null) {
				return new RespMsg_None();
			}
			if (Cmd.CMD_Config == command) {
				return new RespMsg_Config().setCmd(command).as();
			} else if (Cmd.CMD_StartCollection == command) {
				return new RespMsg_StartCollection().setCmd(command).as();
			} else if (Cmd.CMD_StartUpload == command) {
				return new RespMsg_StartUpload().setCmd(command).as();
			} else if (Cmd.CMD_StopUpload == command) {
				return new RespMsg_StopUpload().setCmd(command).as();
			} else if (Cmd.CMD_GetStatus == command) {
				return new RespMsg_GetStatus().setCmd(command).as();
			} else if (Cmd.CMD_GetMac == command) {
				return new RespMsg_GetMac().setCmd(command).as();
			} else if (Cmd.CMD_GetPin == command) {
				return new RespMsg_GetPin().setCmd(command).as();
			} else if (Cmd.CMD_StopCollection == command) {
				return new RespMsg_StopCollection().setCmd(command).as();
			} else if (Cmd.CMD_Upload_start == command) {
				return new RespMsg_UploadStart().setCmd(command).as();
			} else if (Cmd.CMD_Upload_data == command) {
				return new RespMsg_UploadData().setCmd(command).as();
			}
		}
		return new RespMsg_None();
	}
}
